/*
 * Code generated for Simulink model VCUComposition_demo.
 *
 * FILE    : VCUComposition_demo.c
 *
 * VERSION : 1.86
 *
 * DATE    : Sun Sep  8 01:03:14 2024
 *
 * Copyright 2011-2017 ECUCoder. All Rights Reserved.
 */

#include "VCUComposition_demo.h"
#include "VCUComposition_demo_private.h"

/* #include "myinclude.h" */

/* Named constants for Chart: '<S1>/Chart' */
#define VCUComposition_demo_IN_AskN    ((uint8_T)1U)
#define VCUComposition_demo_IN_D       ((uint8_T)2U)
#define VCUComposition_demo_IN_Init    ((uint8_T)3U)
#define VCUComposition_demo_IN_N       ((uint8_T)4U)
#define VCUComposition_demo_IN_N_D     ((uint8_T)5U)
#define VCUComposition_demo_IN_Wait    ((uint8_T)6U)
#define VCUComposition_demo_IN_WaitMCU ((uint8_T)7U)
#define VCUComposition_demo_IN_WaitN   ((uint8_T)8U)

boolean L9826VAR701[8]= { 0, 0, 0, 0, 0, 0, 0, 0 };

boolean L9826DIAG701[8]= { 0, 0, 0, 0, 0, 0, 0, 0 };

boolean L9826VAR702[8]= { 0, 0, 0, 0, 0, 0, 0, 0 };

boolean L9826DIAG702[8]= { 0, 0, 0, 0, 0, 0, 0, 0 };

/* Exported block signals */
real32_T APPS_PctAccPedlRaw;           /* '<S32>/Data Type Conversion' */
GearFSMActType SatCov_stGearFSMActvSt; /* '<S1>/GearFSM' */
uint16_T VCUInHW_uAPP1;                /* '<S5>/AnalogInput' */
uint16_T VCUInHW_uAPP2;                /* '<S5>/AnalogInput1' */
uint16_T VCUInHW_uBkP;                 /* '<S5>/AnalogInput2' */
boolean_T VCUInHW_b99MRLS0A4st;        /* '<S5>/PowerDriverState1' */

/* Exported block parameters */
#define  CALIBRATION_SEG_Flash  __attribute__ ((section(".CalPageFlash"))) //Flash内存
#define  CALIBRATION_SEG_RAM  __attribute__ ((section(".CalPageRAM")))    //RAM内存
real32_T CALIBRATION_SEG_RAM Cal_APPS_PctAccrPedlRaw_Pctg_v[2] = { 100.0F, 0.0F } ;
                                     /* Variable: CALIBRATION_SEG_RAM Cal_APPS_PctAccrPedlRaw_Pctg_v
                                      * Referenced by:
                                      *   '<S32>/1-D Lookup Table'
                                      *   '<S32>/1-D Lookup Table2'
                                      */

real32_T CALIBRATION_SEG_RAM Cal_APPS_PctAccrPedlRaw_mv_x[2] = { 250.0F, 4750.0F } ;/* Variable: CALIBRATION_SEG_RAM Cal_APPS_PctAccrPedlRaw_mv_x
                                                                 * Referenced by:
                                                                 *   '<S32>/1-D Lookup Table'
                                                                 *   '<S32>/1-D Lookup Table2'
                                                                 */

uint16_T CALIBRATION_SEG_RAM Cal_APPS_uAPPmvHWmax_K = 4250U;/* Variable: CALIBRATION_SEG_RAM Cal_APPS_uAPPmvHWmax_K
                                         * Referenced by: '<S31>/Constant'
                                         */
uint16_T CALIBRATION_SEG_RAM Cal_APPS_uAPPmvHWmin_K = 250U;/* Variable: CALIBRATION_SEG_RAM Cal_APPS_uAPPmvHWmin_K
                                        * Referenced by:
                                        *   '<S31>/Constant1'
                                        *   '<S32>/Constant'
                                        */
boolean_T CALIBRATION_SEG_RAM Cal_VCUOutHw_bMstRlyEn_RepFlg = 0;
                                      /* Variable: CALIBRATION_SEG_RAM Cal_VCUOutHw_bMstRlyEn_RepFlg
                                       * Referenced by: '<S6>/Constant1'
                                       */
boolean_T CALIBRATION_SEG_RAM Cal_VCUOutHw_bMstRlyEn_RepVal = 0;
                                      /* Variable: CALIBRATION_SEG_RAM Cal_VCUOutHw_bMstRlyEn_RepVal
                                       * Referenced by: '<S6>/Constant3'
                                       */

real32_T CALIBRATION_SEG_Flash FLSCal_APPS_PctAccrPedlRaw_Pctg_v[2] = { 100.0F, 0.0F } ;
                                     /* Variable: CALIBRATION_SEG_Flash FLSCal_APPS_PctAccrPedlRaw_Pctg_v
                                      * Referenced by:
                                      *   '<S32>/1-D Lookup Table'
                                      *   '<S32>/1-D Lookup Table2'
                                      */

real32_T CALIBRATION_SEG_Flash FLSCal_APPS_PctAccrPedlRaw_mv_x[2] = { 250.0F, 4750.0F } ;/* Variable: CALIBRATION_SEG_Flash FLSCal_APPS_PctAccrPedlRaw_mv_x
                                                                 * Referenced by:
                                                                 *   '<S32>/1-D Lookup Table'
                                                                 *   '<S32>/1-D Lookup Table2'
                                                                 */

uint16_T CALIBRATION_SEG_Flash FLSCal_APPS_uAPPmvHWmax_K = 4250U;/* Variable: CALIBRATION_SEG_Flash FLSCal_APPS_uAPPmvHWmax_K
                                         * Referenced by: '<S31>/Constant'
                                         */
uint16_T CALIBRATION_SEG_Flash FLSCal_APPS_uAPPmvHWmin_K = 250U;/* Variable: CALIBRATION_SEG_Flash FLSCal_APPS_uAPPmvHWmin_K
                                        * Referenced by:
                                        *   '<S31>/Constant1'
                                        *   '<S32>/Constant'
                                        */
boolean_T CALIBRATION_SEG_Flash FLSCal_VCUOutHw_bMstRlyEn_RepFlg = 0;
                                      /* Variable: CALIBRATION_SEG_Flash FLSCal_VCUOutHw_bMstRlyEn_RepFlg
                                       * Referenced by: '<S6>/Constant1'
                                       */
boolean_T CALIBRATION_SEG_Flash FLSCal_VCUOutHw_bMstRlyEn_RepVal = 0;
                                      /* Variable: CALIBRATION_SEG_Flash FLSCal_VCUOutHw_bMstRlyEn_RepVal
                                       * Referenced by: '<S6>/Constant3'
                                       */

/* Block signals (default storage) */
B_VCUComposition_demo_T VCUComposition_demo_B;

/* Block states (default storage) */
DW_VCUComposition_demo_T VCUComposition_demo_DW;

/* Real-time model */
static RT_MODEL_VCUComposition_demo_T VCUComposition_demo_M_;
RT_MODEL_VCUComposition_demo_T *const VCUComposition_demo_M =
  &VCUComposition_demo_M_;
static void rate_monotonic_scheduler(void);

/* L9826 control function */
void ec_l9826tr701_control(boolean SPITX[8])
{
  uint8 i;
  uint16 SPI1TX[1]= { 0x00 };

  uint16 SPI1RX[1]= { 0x00 };

  for (i=0;i<8;i++) {
    SPI1TX[0]= SPI1TX[0]|(SPITX[i]<<(7-i));
  }

  ec_spi_masterwriteread(0, 0, 1, SPI1TX, SPI1RX);
  L9826DIAG701[0] = (SPI1RX[0]&0b10000000)>>7;
  L9826DIAG701[1] = (SPI1RX[0]&0b01000000)>>6;
  L9826DIAG701[2] = (SPI1RX[0]&0b00100000)>>5;
  L9826DIAG701[3] = (SPI1RX[0]&0b00010000)>>4;
  L9826DIAG701[4] = (SPI1RX[0]&0b00001000)>>3;
  L9826DIAG701[5] = (SPI1RX[0]&0b00000100)>>2;
  L9826DIAG701[6] = (SPI1RX[0]&0b00000010)>>1;
  L9826DIAG701[7] = (SPI1RX[0]&0b00000001)>>0;
}

void ec_l9826tr702_control(boolean SPITX[8])
{
  uint8 i;
  uint16 SPI1TX[1]= { 0x00 };

  uint16 SPI1RX[1]= { 0x00 };

  for (i=0;i<8;i++) {
    SPI1TX[0]= SPI1TX[0]|(SPITX[i]<<(7-i));
  }

  ec_spi_masterwriteread(0, 1, 1, SPI1TX, SPI1RX);
  L9826DIAG702[0] = (SPI1RX[0]&0b10000000)>>7;
  L9826DIAG702[1] = (SPI1RX[0]&0b01000000)>>6;
  L9826DIAG702[2] = (SPI1RX[0]&0b00100000)>>5;
  L9826DIAG702[3] = (SPI1RX[0]&0b00010000)>>4;
  L9826DIAG702[4] = (SPI1RX[0]&0b00001000)>>3;
  L9826DIAG702[5] = (SPI1RX[0]&0b00000100)>>2;
  L9826DIAG702[6] = (SPI1RX[0]&0b00000010)>>1;
  L9826DIAG702[7] = (SPI1RX[0]&0b00000001)>>0;
}

void ISR_PIT_CH3(void)
{
  PIT_0.TIMER[3].TFLG.R = 1;
  ECUCoderModelBaseCounter++;
  rate_monotonic_scheduler();
}

uint16_T look1_iu16bflfyu16tf_binlcs(uint16_T u0, const real32_T bp0[], const
  real32_T table[], uint32_T maxIndex)
{
  real32_T frac;
  real32_T yL_0d0;
  uint32_T bpIdx;
  uint32_T iLeft;
  uint32_T iRght;

  /* Column-major Lookup 1-D
     Search method: 'binary'
     Use previous index: 'off'
     Interpolation method: 'Linear point-slope'
     Extrapolation method: 'Clip'
     Use last breakpoint for index at or above upper limit: 'off'
     Remove protection against out-of-range input in generated code: 'off'
     Rounding mode: 'simplest'
   */
  /* Prelookup - Index and Fraction
     Index Search method: 'binary'
     Extrapolation method: 'Clip'
     Use previous index: 'off'
     Use last breakpoint for index at or above upper limit: 'off'
     Remove protection against out-of-range input in generated code: 'off'
   */
  if (u0 < bp0[0U]) {
    iLeft = 0U;
    frac = 0.0F;
  } else if (u0 < bp0[maxIndex]) {
    /* Binary Search */
    bpIdx = maxIndex >> 1U;
    iLeft = 0U;
    iRght = maxIndex;
    while (iRght - iLeft > 1U) {
      if (u0 < bp0[bpIdx]) {
        iRght = bpIdx;
      } else {
        iLeft = bpIdx;
      }

      bpIdx = (iRght + iLeft) >> 1U;
    }

    frac = ((real32_T)u0 - bp0[iLeft]) / (bp0[iLeft + 1U] - bp0[iLeft]);
  } else {
    iLeft = maxIndex - 1U;
    frac = 1.0F;
  }

  /* Column-major Interpolation 1-D
     Interpolation method: 'Linear point-slope'
     Use last breakpoint for index at or above upper limit: 'off'
     Overflow mode: 'wrapping'
   */
  yL_0d0 = table[iLeft];
  return (uint16_T)((table[iLeft + 1U] - yL_0d0) * frac + yL_0d0);
}

void ISR_FlexCAN_2_MB0(void)
{
  /* Call the system: <S14>/CCPReceive */
  {
    /* S-Function (ec5744_caninterruptslb1): '<S14>/ReceiveandTransmitInterrupt' */

    /* Output and update for function-call system: '<S14>/CCPReceive' */

    /* S-Function (ec5744_canreceiveslb): '<S29>/CANReceive' */

    /* Receive CAN message */
    {
      uint8 CAN2BUF0RX[8]= { 0, 0, 0, 0, 0, 0, 0, 0 };

      uint8 can2buf0looprx= 0;
      VCUComposition_demo_B.CANReceive_o3= 256;
      VCUComposition_demo_B.CANReceive_o5= 8;
      VCUComposition_demo_B.CANReceive_o2= ec_can_receive(2,0, CAN2BUF0RX);
      VCUComposition_demo_B.CANReceive_o4[0]= CAN2BUF0RX[can2buf0looprx];
      can2buf0looprx++;
      VCUComposition_demo_B.CANReceive_o4[1]= CAN2BUF0RX[can2buf0looprx];
      can2buf0looprx++;
      VCUComposition_demo_B.CANReceive_o4[2]= CAN2BUF0RX[can2buf0looprx];
      can2buf0looprx++;
      VCUComposition_demo_B.CANReceive_o4[3]= CAN2BUF0RX[can2buf0looprx];
      can2buf0looprx++;
      VCUComposition_demo_B.CANReceive_o4[4]= CAN2BUF0RX[can2buf0looprx];
      can2buf0looprx++;
      VCUComposition_demo_B.CANReceive_o4[5]= CAN2BUF0RX[can2buf0looprx];
      can2buf0looprx++;
      VCUComposition_demo_B.CANReceive_o4[6]= CAN2BUF0RX[can2buf0looprx];
      can2buf0looprx++;
      VCUComposition_demo_B.CANReceive_o4[7]= CAN2BUF0RX[can2buf0looprx];
      can2buf0looprx++;
    }

    /* Nothing to do for system: <S29>/Nothing */

    /* End of Outputs for S-Function (ec5744_canreceiveslb): '<S29>/CANReceive' */

    /* End of Outputs for S-Function (ec5744_caninterruptslb1): '<S14>/ReceiveandTransmitInterrupt' */
  }

  FLEXCAN(2).IFLAG1.B.BUF0I = 1;
                               /* Clear CAN interrupt flag by writing it to 1 */
}

/*
 * Set which subrates need to run this base step (base rate always runs).
 * This function must be called prior to calling the model step function
 * in order to "remember" which rates need to run this base step.  The
 * buffering of events allows for overlapping preemption.
 */
void VCUComposition_demo_SetEventsForThisBaseStep(boolean_T *eventFlags)
{
  /* Task runs when its counter is zero, computed via rtmStepTask macro */
  eventFlags[1] = ((boolean_T)rtmStepTask(VCUComposition_demo_M, 1));
  eventFlags[2] = ((boolean_T)rtmStepTask(VCUComposition_demo_M, 2));
  eventFlags[3] = ((boolean_T)rtmStepTask(VCUComposition_demo_M, 3));
  eventFlags[4] = ((boolean_T)rtmStepTask(VCUComposition_demo_M, 4));
  eventFlags[5] = ((boolean_T)rtmStepTask(VCUComposition_demo_M, 5));
  eventFlags[6] = ((boolean_T)rtmStepTask(VCUComposition_demo_M, 6));
  eventFlags[7] = ((boolean_T)rtmStepTask(VCUComposition_demo_M, 7));
}

/*
 *   This function updates active task flag for each subrate
 * and rate transition flags for tasks that exchange data.
 * The function assumes rate-monotonic multitasking scheduler.
 * The function must be called at model base rate so that
 * the generated code self-manages all its subrates and rate
 * transition flags.
 */
static void rate_monotonic_scheduler(void)
{
  /* To ensure a deterministic data transfer between two rates,
   * data is transferred at the priority of a fast task and the frequency
   * of the slow task.  The following flags indicate when the data transfer
   * happens.  That is, a rate interaction flag is set true when both rates
   * will run, and false otherwise.
   */

  /* tid 1 shares data with slower tid rate: 2 */
  if (VCUComposition_demo_M->Timing.TaskCounters.TID[1] == 0) {
    VCUComposition_demo_M->Timing.RateInteraction.TID1_2 =
      (VCUComposition_demo_M->Timing.TaskCounters.TID[2] == 0);
  }

  /* tid 2 shares data with slower tid rate: 4 */
  if (VCUComposition_demo_M->Timing.TaskCounters.TID[2] == 0) {
    VCUComposition_demo_M->Timing.RateInteraction.TID2_4 =
      (VCUComposition_demo_M->Timing.TaskCounters.TID[4] == 0);
  }

  /* Compute which subrates run during the next base time step.  Subrates
   * are an integer multiple of the base rate counter.  Therefore, the subtask
   * counter is reset when it reaches its limit (zero means run).
   */
  (VCUComposition_demo_M->Timing.TaskCounters.TID[1])++;
  if ((VCUComposition_demo_M->Timing.TaskCounters.TID[1]) > 1) {/* Sample time: [0.001s, 0.0s] */
    VCUComposition_demo_M->Timing.TaskCounters.TID[1] = 0;
  }

  (VCUComposition_demo_M->Timing.TaskCounters.TID[2])++;
  if ((VCUComposition_demo_M->Timing.TaskCounters.TID[2]) > 3) {/* Sample time: [0.002s, 0.0s] */
    VCUComposition_demo_M->Timing.TaskCounters.TID[2] = 0;
  }

  (VCUComposition_demo_M->Timing.TaskCounters.TID[3])++;
  if ((VCUComposition_demo_M->Timing.TaskCounters.TID[3]) > 9) {/* Sample time: [0.005s, 0.0s] */
    VCUComposition_demo_M->Timing.TaskCounters.TID[3] = 0;
  }

  (VCUComposition_demo_M->Timing.TaskCounters.TID[4])++;
  if ((VCUComposition_demo_M->Timing.TaskCounters.TID[4]) > 19) {/* Sample time: [0.01s, 0.0s] */
    VCUComposition_demo_M->Timing.TaskCounters.TID[4] = 0;
  }

  (VCUComposition_demo_M->Timing.TaskCounters.TID[5])++;
  if ((VCUComposition_demo_M->Timing.TaskCounters.TID[5]) > 99) {/* Sample time: [0.05s, 0.0s] */
    VCUComposition_demo_M->Timing.TaskCounters.TID[5] = 0;
  }

  (VCUComposition_demo_M->Timing.TaskCounters.TID[6])++;
  if ((VCUComposition_demo_M->Timing.TaskCounters.TID[6]) > 199) {/* Sample time: [0.1s, 0.0s] */
    VCUComposition_demo_M->Timing.TaskCounters.TID[6] = 0;
  }

  (VCUComposition_demo_M->Timing.TaskCounters.TID[7])++;
  if ((VCUComposition_demo_M->Timing.TaskCounters.TID[7]) > 999) {/* Sample time: [0.5s, 0.0s] */
    VCUComposition_demo_M->Timing.TaskCounters.TID[7] = 0;
  }
}

/* Model step function for TID0 */
void VCUComposition_demo_step0(void)   /* Sample time: [0.0005s, 0.0s] */
{
  {                                    /* Sample time: [0.0005s, 0.0s] */
    rate_monotonic_scheduler();
  }
}

/* Model step function for TID1 */
void VCUComposition_demo_step1(void)   /* Sample time: [0.001s, 0.0s] */
{
  /* RateTransition: '<Root>/Rate Transition3' incorporates:
   *  Constant: '<Root>/Constant'
   */
  if (VCUComposition_demo_M->Timing.RateInteraction.TID1_2) {
    VCUComposition_demo_DW.RateTransition3_Buffer = false;
  }

  /* End of RateTransition: '<Root>/Rate Transition3' */

  /* S-Function (fcgen): '<S14>/Function-Call Generator' incorporates:
   *  SubSystem: '<S14>/CCPBackground'
   */
  /* S-Function (ec5744_ccpslb): '<S28>/CCPBackground' */
  ccpBackground();
  Lin0_Background();

  /* End of Outputs for S-Function (fcgen): '<S14>/Function-Call Generator' */
}

/* Model step function for TID2 */
void VCUComposition_demo_step2(void)   /* Sample time: [0.002s, 0.0s] */
{
  boolean_T rtb_RateTransition3;

  /* RateTransition: '<Root>/Rate Transition3' */
  rtb_RateTransition3 = VCUComposition_demo_DW.RateTransition3_Buffer;

  /* S-Function (fcgen): '<Root>/VCU_2msTask2' incorporates:
   *  SubSystem: '<Root>/VCUOutHW'
   */
  /* Switch: '<S41>/Switch' incorporates:
   *  Constant: '<S6>/Constant1'
   */
  if (Cal_VCUOutHw_bMstRlyEn_RepFlg) {
    /* Switch: '<S41>/Switch' incorporates:
     *  Constant: '<S6>/Constant3'
     */
    VCUComposition_demo_B.Switch = Cal_VCUOutHw_bMstRlyEn_RepVal;
  } else {
    /* Switch: '<S41>/Switch' */
    VCUComposition_demo_B.Switch = rtb_RateTransition3;
  }

  /* End of Switch: '<S41>/Switch' */

  /* S-Function (ec5744_pdsslb2u3): '<S6>/PowerDriverSwitch(LS)1' */
  L9826VAR702[2]= VCUComposition_demo_B.Switch;
  ec_l9826tr702_control(L9826VAR702);

  /* End of Outputs for S-Function (fcgen): '<Root>/VCU_2msTask2' */

  /* S-Function (fcgen): '<Root>/VCU_2msTask1' incorporates:
   *  SubSystem: '<Root>/VCUInHW'
   */
  /* S-Function (ec5744_asislbu3): '<S5>/AnalogInput' */

  /* Read the ADC conversion result of the analog signal */
  VCUInHW_uAPP1= adc_read_chan(1,4);

  /* S-Function (ec5744_asislbu3): '<S5>/AnalogInput1' */

  /* Read the ADC conversion result of the analog signal */
  VCUInHW_uAPP2= adc_read_chan(1,2);

  /* S-Function (ec5744_asislbu3): '<S5>/AnalogInput2' */

  /* Read the ADC conversion result of the analog signal */
  VCUInHW_uBkP= adc_read_chan(1,0);

  /* S-Function (ec5744_pdssslbu3): '<S5>/PowerDriverState1' */
  VCUInHW_b99MRLS0A4st= L9826DIAG702[2];

  /* End of Outputs for S-Function (fcgen): '<Root>/VCU_2msTask1' */

  /* RateTransition: '<Root>/Rate Transition5' */
  if (VCUComposition_demo_M->Timing.RateInteraction.TID2_4) {
    VCUComposition_demo_DW.RateTransition5_Buffer = VCUInHW_uAPP1;

    /* RateTransition: '<Root>/Rate Transition6' */
    VCUComposition_demo_DW.RateTransition6_Buffer = VCUInHW_uAPP2;
  }

  /* End of RateTransition: '<Root>/Rate Transition5' */
}

/* Model step function for TID3 */
void VCUComposition_demo_step3(void)   /* Sample time: [0.005s, 0.0s] */
{
  /* S-Function (fcgen): '<S12>/5ms' incorporates:
   *  SubSystem: '<S12>/daq5ms'
   */

  /* S-Function (ec5744_ccpslb1): '<S26>/CCPDAQ' */
  ccpDaq(0);

  /* End of Outputs for S-Function (fcgen): '<S12>/5ms' */
}

/* Model step function for TID4 */
void VCUComposition_demo_step4(void)   /* Sample time: [0.01s, 0.0s] */
{
  real_T rtb_Min1;
  uint32_T rtb_Gain;
  uint16_T rtb_RateTransition5;
  uint16_T rtb_RateTransition6;
  uint16_T rtb_Switch;
  boolean_T rtb_APPS_bAPPmvSCSVld_idx_0;
  boolean_T rtb_APPS_bAPPmvSCSVld_idx_1;
  boolean_T rtb_Switch3_idx_0;
  boolean_T rtb_Switch3_idx_1;

  /* RateTransition: '<Root>/Rate Transition5' */
  rtb_RateTransition5 = VCUComposition_demo_DW.RateTransition5_Buffer;

  /* RateTransition: '<Root>/Rate Transition6' */
  rtb_RateTransition6 = VCUComposition_demo_DW.RateTransition6_Buffer;

  /* S-Function (fcgen): '<Root>/VCU_10msTask1' incorporates:
   *  SubSystem: '<Root>/Subsystem Reference'
   */
  /* RelationalOperator: '<S33>/Lower Test' incorporates:
   *  Constant: '<S31>/Constant1'
   */
  rtb_Switch3_idx_0 = (Cal_APPS_uAPPmvHWmin_K <= rtb_RateTransition5);
  rtb_Switch3_idx_1 = (Cal_APPS_uAPPmvHWmin_K <= rtb_RateTransition6);

  /* RelationalOperator: '<S33>/Upper Test' incorporates:
   *  Constant: '<S31>/Constant'
   */
  rtb_APPS_bAPPmvSCSVld_idx_0 = (rtb_RateTransition5 <= Cal_APPS_uAPPmvHWmax_K);
  rtb_APPS_bAPPmvSCSVld_idx_1 = (rtb_RateTransition6 <= Cal_APPS_uAPPmvHWmax_K);

  /* Switch: '<S35>/Switch' */
  rtb_Switch = rtb_RateTransition5;

  /* Sum: '<S34>/Add' incorporates:
   *  UnitDelay: '<S34>/Unit Delay'
   */
  rtb_Min1 = VCUComposition_demo_DW.UnitDelay_DSTATE[0] + 1.0;

  /* Logic: '<S33>/FixPt Logical Operator' */
  rtb_Switch3_idx_0 = (rtb_Switch3_idx_0 && rtb_APPS_bAPPmvSCSVld_idx_0);

  /* Sum: '<S34>/Add1' incorporates:
   *  UnitDelay: '<S34>/Unit Delay'
   */
  VCUComposition_demo_DW.UnitDelay_DSTATE[0]--;

  /* Switch: '<S34>/Switch' incorporates:
   *  UnitDelay: '<S34>/Unit Delay'
   */
  if (!rtb_Switch3_idx_0) {
    rtb_Min1 = VCUComposition_demo_DW.UnitDelay_DSTATE[0];
  }

  /* RelationalOperator: '<S34>/Relational Operator' */
  rtb_APPS_bAPPmvSCSVld_idx_0 = (rtb_Min1 <= 0.0);

  /* RelationalOperator: '<S34>/Relational Operator1' */
  rtb_Switch3_idx_0 = (rtb_Min1 >= 3.0);

  /* Switch: '<S34>/Switch3' incorporates:
   *  UnitDelay: '<S34>/Unit Delay1'
   */
  rtb_Switch3_idx_0 = (rtb_Switch3_idx_0 ||
                       VCUComposition_demo_DW.UnitDelay1_DSTATE[0]);

  /* Switch: '<S34>/Switch4' incorporates:
   *  UnitDelay: '<S34>/Unit Delay1'
   */
  VCUComposition_demo_DW.UnitDelay1_DSTATE[0] = ((!rtb_APPS_bAPPmvSCSVld_idx_0) &&
    rtb_Switch3_idx_0);

  /* DataTypeConversion: '<S31>/Data Type Conversion' incorporates:
   *  UnitDelay: '<S34>/Unit Delay1'
   */
  rtb_APPS_bAPPmvSCSVld_idx_0 = VCUComposition_demo_DW.UnitDelay1_DSTATE[0];

  /* Switch: '<S35>/Switch' incorporates:
   *  Constant: '<S32>/Constant'
   */
  if (rtb_APPS_bAPPmvSCSVld_idx_0) {
    rtb_Switch = Cal_APPS_uAPPmvHWmin_K;
  }

  /* MinMax: '<S34>/Max1' */
  rtb_Min1 = fmax(0.0, rtb_Min1);

  /* MinMax: '<S34>/Min1' */
  rtb_Min1 = fmin(3.0, rtb_Min1);

  /* Switch: '<S34>/Switch1' incorporates:
   *  UnitDelay: '<S34>/Unit Delay'
   */
  VCUComposition_demo_DW.UnitDelay_DSTATE[0] = rtb_Min1;

  /* Switch: '<S35>/Switch' */
  rtb_RateTransition5 = rtb_Switch;
  rtb_Switch = rtb_RateTransition6;

  /* Logic: '<S33>/FixPt Logical Operator' */
  rtb_APPS_bAPPmvSCSVld_idx_0 = rtb_APPS_bAPPmvSCSVld_idx_1;
  rtb_Switch3_idx_0 = rtb_Switch3_idx_1;

  /* Sum: '<S34>/Add' incorporates:
   *  UnitDelay: '<S34>/Unit Delay'
   */
  rtb_Min1 = VCUComposition_demo_DW.UnitDelay_DSTATE[1] + 1.0;

  /* Logic: '<S33>/FixPt Logical Operator' */
  rtb_Switch3_idx_0 = (rtb_Switch3_idx_0 && rtb_APPS_bAPPmvSCSVld_idx_0);

  /* Sum: '<S34>/Add1' incorporates:
   *  UnitDelay: '<S34>/Unit Delay'
   */
  VCUComposition_demo_DW.UnitDelay_DSTATE[1]--;

  /* Switch: '<S34>/Switch' incorporates:
   *  UnitDelay: '<S34>/Unit Delay'
   */
  if (!rtb_Switch3_idx_0) {
    rtb_Min1 = VCUComposition_demo_DW.UnitDelay_DSTATE[1];
  }

  /* RelationalOperator: '<S34>/Relational Operator' */
  rtb_APPS_bAPPmvSCSVld_idx_0 = (rtb_Min1 <= 0.0);

  /* RelationalOperator: '<S34>/Relational Operator1' */
  rtb_Switch3_idx_0 = (rtb_Min1 >= 3.0);

  /* Switch: '<S34>/Switch3' incorporates:
   *  UnitDelay: '<S34>/Unit Delay1'
   */
  rtb_Switch3_idx_0 = (rtb_Switch3_idx_0 ||
                       VCUComposition_demo_DW.UnitDelay1_DSTATE[1]);

  /* Switch: '<S34>/Switch4' incorporates:
   *  UnitDelay: '<S34>/Unit Delay1'
   */
  VCUComposition_demo_DW.UnitDelay1_DSTATE[1] = ((!rtb_APPS_bAPPmvSCSVld_idx_0) &&
    rtb_Switch3_idx_0);

  /* DataTypeConversion: '<S31>/Data Type Conversion' incorporates:
   *  UnitDelay: '<S34>/Unit Delay1'
   */
  rtb_APPS_bAPPmvSCSVld_idx_0 = VCUComposition_demo_DW.UnitDelay1_DSTATE[1];

  /* Switch: '<S35>/Switch' incorporates:
   *  Constant: '<S32>/Constant'
   */
  if (rtb_APPS_bAPPmvSCSVld_idx_0) {
    rtb_Switch = Cal_APPS_uAPPmvHWmin_K;
  }

  /* MinMax: '<S34>/Max1' */
  rtb_Min1 = fmax(0.0, rtb_Min1);

  /* MinMax: '<S34>/Min1' */
  rtb_Min1 = fmin(3.0, rtb_Min1);

  /* Switch: '<S34>/Switch1' incorporates:
   *  UnitDelay: '<S34>/Unit Delay'
   */
  VCUComposition_demo_DW.UnitDelay_DSTATE[1] = rtb_Min1;

  /* Switch: '<S35>/Switch' */
  rtb_RateTransition6 = rtb_Switch;

  /* Lookup_n-D: '<S32>/1-D Lookup Table' */
  rtb_RateTransition5 = look1_iu16bflfyu16tf_binlcs(rtb_RateTransition5,
    Cal_APPS_PctAccrPedlRaw_mv_x, Cal_APPS_PctAccrPedlRaw_Pctg_v, 1U);

  /* Lookup_n-D: '<S32>/1-D Lookup Table2' */
  rtb_RateTransition6 = look1_iu16bflfyu16tf_binlcs(rtb_RateTransition6,
    Cal_APPS_PctAccrPedlRaw_mv_x, Cal_APPS_PctAccrPedlRaw_Pctg_v, 1U);

  /* Sum: '<S32>/Add' */
  rtb_RateTransition5 = (uint16_T)((uint32_T)rtb_RateTransition5 +
    rtb_RateTransition6);

  /* Gain: '<S32>/Gain' */
  rtb_Gain = (uint32_T)rtb_RateTransition5 << 15;

  /* DataTypeConversion: '<S32>/Data Type Conversion' incorporates:
   *  Gain: '<S32>/Gain'
   */
  APPS_PctAccPedlRaw = (real32_T)rtb_Gain * 1.52587891E-5F;

  /* End of Outputs for S-Function (fcgen): '<Root>/VCU_10msTask1' */

  /* S-Function (fcgen): '<S12>/10ms' incorporates:
   *  SubSystem: '<S12>/daq10ms'
   */
  /* S-Function (ec5744_ccpslb1): '<S24>/CCPDAQ' */
  ccpDaq(1);

  /* End of Outputs for S-Function (fcgen): '<S12>/10ms' */
}

/* Model step function for TID5 */
void VCUComposition_demo_step5(void)   /* Sample time: [0.05s, 0.0s] */
{
  uint32_T elapsedTicks;

  /* S-Function (fcgen): '<Root>/VCU_50msTask6' incorporates:
   *  SubSystem: '<Root>/Function-Call Subsystem3'
   */
  /* Chart: '<S1>/Chart' */
  elapsedTicks = VCUComposition_demo_M->Timing.clockTick5 -
    VCUComposition_demo_DW.previousTicks;
  VCUComposition_demo_DW.previousTicks =
    VCUComposition_demo_M->Timing.clockTick5;
  if (VCUComposition_demo_DW.temporalCounter_i1 + elapsedTicks <= 63U) {
    VCUComposition_demo_DW.temporalCounter_i1 = (uint8_T)
      (VCUComposition_demo_DW.temporalCounter_i1 + elapsedTicks);
  } else {
    VCUComposition_demo_DW.temporalCounter_i1 = 63U;
  }

  if (VCUComposition_demo_DW.bitsForTID5.is_active_c3_VCUComposition_dem == 0U)
  {
    VCUComposition_demo_DW.bitsForTID5.is_active_c3_VCUComposition_dem = 1U;
    VCUComposition_demo_DW.bitsForTID5.is_c3_VCUComposition_demo =
      VCUComposition_demo_IN_Init;
    VCUComposition_demo_DW.temporalCounter_i1 = 0U;
  } else {
    switch (VCUComposition_demo_DW.bitsForTID5.is_c3_VCUComposition_demo) {
     case VCUComposition_demo_IN_AskN:
      VCUComposition_demo_DW.bitsForTID5.is_c3_VCUComposition_demo =
        VCUComposition_demo_IN_WaitN;
      break;

     case VCUComposition_demo_IN_D:
      VCUComposition_demo_DW.bitsForTID5.is_c3_VCUComposition_demo =
        VCUComposition_demo_IN_N;
      break;

     case VCUComposition_demo_IN_Init:
      VCUComposition_demo_DW.bitsForTID5.is_c3_VCUComposition_demo =
        VCUComposition_demo_IN_Wait;
      break;

     case VCUComposition_demo_IN_N:
     case VCUComposition_demo_IN_WaitMCU:
      break;

     case VCUComposition_demo_IN_N_D:
      VCUComposition_demo_DW.bitsForTID5.is_c3_VCUComposition_demo =
        VCUComposition_demo_IN_D;
      break;

     case VCUComposition_demo_IN_Wait:
      VCUComposition_demo_DW.bitsForTID5.is_c3_VCUComposition_demo =
        VCUComposition_demo_IN_WaitMCU;
      break;

     default:
      /* case IN_WaitN: */
      VCUComposition_demo_DW.bitsForTID5.is_c3_VCUComposition_demo =
        VCUComposition_demo_IN_D;
      break;
    }
  }

  /* End of Chart: '<S1>/Chart' */

  /* Chart: '<S1>/GearFSM' */
  if (VCUComposition_demo_DW.bitsForTID5.is_active_c1_VCUComposition_dem == 0U)
  {
    VCUComposition_demo_DW.bitsForTID5.is_active_c1_VCUComposition_dem = 1U;
    SatCov_stGearFSMActvSt = GearFSMActType_N;
  } else if (SatCov_stGearFSMActvSt == GearFSMActType_D) {
    SatCov_stGearFSMActvSt = GearFSMActType_N;
  } else {
    /* State N */
  }

  /* End of Chart: '<S1>/GearFSM' */
  /* End of Outputs for S-Function (fcgen): '<Root>/VCU_50msTask6' */

  /* S-Function (fcgen): '<S12>/50ms' incorporates:
   *  SubSystem: '<S12>/daq50ms'
   */
  /* S-Function (ec5744_ccpslb1): '<S25>/CCPDAQ' */
  ccpDaq(2);

  /* End of Outputs for S-Function (fcgen): '<S12>/50ms' */

  /* Update absolute time */
  /* The "clockTick5" counts the number of times the code of this task has
   * been executed. The resolution of this integer timer is 0.05, which is the step size
   * of the task. Size of "clockTick5" ensures timer will not overflow during the
   * application lifespan selected.
   */
  VCUComposition_demo_M->Timing.clockTick5++;
}

/* Model step function for TID6 */
void VCUComposition_demo_step6(void)   /* Sample time: [0.1s, 0.0s] */
{
  /* S-Function (fcgen): '<S11>/100MS' incorporates:
   *  SubSystem: '<S11>/Function-Call Subsystem'
   */
  /* S-Function (ec5744_canreceiveslb): '<S15>/CANReceive' */

  /* Receive CAN message */
  {
    uint8 CAN2BUF1RX[8]= { 0, 0, 0, 0, 0, 0, 0, 0 };

    uint8 can2buf1looprx= 0;
    VCUComposition_demo_B.CANReceive_o3_c= 278;
    VCUComposition_demo_B.CANReceive_o5_k= 8;
    VCUComposition_demo_B.CANReceive_o2_p= ec_can_receive(2,1, CAN2BUF1RX);
    VCUComposition_demo_B.CANReceive_o4_j[0]= CAN2BUF1RX[can2buf1looprx];
    can2buf1looprx++;
    VCUComposition_demo_B.CANReceive_o4_j[1]= CAN2BUF1RX[can2buf1looprx];
    can2buf1looprx++;
    VCUComposition_demo_B.CANReceive_o4_j[2]= CAN2BUF1RX[can2buf1looprx];
    can2buf1looprx++;
    VCUComposition_demo_B.CANReceive_o4_j[3]= CAN2BUF1RX[can2buf1looprx];
    can2buf1looprx++;
    VCUComposition_demo_B.CANReceive_o4_j[4]= CAN2BUF1RX[can2buf1looprx];
    can2buf1looprx++;
    VCUComposition_demo_B.CANReceive_o4_j[5]= CAN2BUF1RX[can2buf1looprx];
    can2buf1looprx++;
    VCUComposition_demo_B.CANReceive_o4_j[6]= CAN2BUF1RX[can2buf1looprx];
    can2buf1looprx++;
    VCUComposition_demo_B.CANReceive_o4_j[7]= CAN2BUF1RX[can2buf1looprx];
    can2buf1looprx++;
  }

  /* Call the system: <S15>/Function-Call Subsystem */

  /* Output and update for function-call system: '<S15>/Function-Call Subsystem' */
  {
    uint8_T rtb_Add;
    uint8_T rtb_Compare;

    /* Outputs for Enabled SubSystem: '<S16>/Enabled Subsystem' incorporates:
     *  EnablePort: '<S17>/Enable'
     */
    if (VCUComposition_demo_B.CANReceive_o2_p > 0) {
      /* RelationalOperator: '<S18>/Compare' incorporates:
       *  Constant: '<S18>/Constant'
       */
      rtb_Add = (uint8_T)(VCUComposition_demo_B.CANReceive_o4_j[0] == 83);

      /* RelationalOperator: '<S19>/Compare' incorporates:
       *  Constant: '<S19>/Constant'
       */
      rtb_Compare = (uint8_T)(VCUComposition_demo_B.CANReceive_o4_j[5] == 84);

      /* Sum: '<S17>/Add' */
      rtb_Add = (uint8_T)((uint32_T)rtb_Add + rtb_Compare);

      /* RelationalOperator: '<S20>/Compare' incorporates:
       *  Constant: '<S20>/Constant'
       */
      rtb_Compare = (uint8_T)(rtb_Add == 2);

      /* If: '<S17>/If' */
      if (rtb_Compare > 0) {
        /* Outputs for IfAction SubSystem: '<S17>/If Action Subsystem' incorporates:
         *  ActionPort: '<S21>/Action Port'
         */
        /* S-Function (ec5744_bootloaderslb): '<S21>/BootLoader' */
        {
          uint16 i= 0;
          N256K_BLOCK_SEL n256KBlockSelect;
          CONTEXT_DATA pgmCtxData;
          n256KBlockSelect.first256KBlockSelect = 0x00000080;
          n256KBlockSelect.second256KBlockSelect = 0x00000000;
          pgmCtxData.pReqCompletionFn = pFlashProgram;
          uint8 CAN2BUF9TX[]= { 11, 12, 12, 13 };

          uint8 returnCode1= 0;
          uint8 bootflag[]= { 1, 0, 0, 0, 0, 0, 0, 0 };

          DisableInterrupts();
          SIUL2.GPDO[22].R = 1;
          App_FlashErase( &ssdConfig, 0, 0x00000000, 0x00000000, 0x00000000,
                         n256KBlockSelect );
          App_FlashProgram( &ssdConfig, 0, 0x011FFFF0, 8, (uint32)bootflag,
                           &pgmCtxData );
          i= 1000;
          while (i--) {
            ;
          }

          ec_can_transmit(2, 9, 0, 593, 4, CAN2BUF9TX);
          i= 10000;
          while (i--) {
            ;
          }
        }

        /* S-Function (ec5744_cpuresetslb): '<S21>/CPUReset' */

        /* Perform a microcontroller reset */
        MC_ME.MCTL.R = 0X00005AF0;
        MC_ME.MCTL.R = 0X0000A50F;

        /* End of Outputs for SubSystem: '<S17>/If Action Subsystem' */
      } else {
        /* Outputs for IfAction SubSystem: '<S17>/If Action Subsystem1' incorporates:
         *  ActionPort: '<S22>/Action Port'
         */
        /* S-Function (ec5744_cantransmitslb): '<S22>/CANTransmit' incorporates:
         *  Constant: '<S22>/Constant'
         */

        /*Transmit CAN message*/
        {
          uint8 CAN2BUF9TX[1];
          uint8 can2buf9looptx= 0;
          CAN2BUF9TX[can2buf9looptx]= ((uint8_T)1U);
          can2buf9looptx++;
          VCUComposition_demo_B.CANTransmit= ec_can_transmit(2, 9, 0, 593U, 1,
            CAN2BUF9TX);
        }

        /* End of Outputs for SubSystem: '<S17>/If Action Subsystem1' */
      }

      /* End of If: '<S17>/If' */
    }

    /* End of Outputs for SubSystem: '<S16>/Enabled Subsystem' */
  }

  /* End of Outputs for S-Function (ec5744_canreceiveslb): '<S15>/CANReceive' */
  /* End of Outputs for S-Function (fcgen): '<S11>/100MS' */

  /* S-Function (fcgen): '<S12>/100ms' incorporates:
   *  SubSystem: '<S12>/daq100ms'
   */
  /* S-Function (ec5744_ccpslb1): '<S23>/CCPDAQ' */
  ccpDaq(3);

  /* End of Outputs for S-Function (fcgen): '<S12>/100ms' */
}

/* Model step function for TID7 */
void VCUComposition_demo_step7(void)   /* Sample time: [0.5s, 0.0s] */
{
  /* S-Function (fcgen): '<S13>/500ms' incorporates:
   *  SubSystem: '<S13>/EEPROMOperation'
   */

  /* S-Function (ec5744_eepromoslb): '<S27>/EEPROMOperatin' */
#if defined EC_EEPROM_ENABLE

  /* Operate the EEPROM module on the MPC5744 */
  ec_flash_operation();

#endif

  /* End of Outputs for S-Function (fcgen): '<S13>/500ms' */
}

/* Model wrapper function for compatibility with a static main program */
void VCUComposition_demo_step(int_T tid)
{
  switch (tid) {
   case 0 :
    VCUComposition_demo_step0();
    break;

   case 1 :
    VCUComposition_demo_step1();
    break;

   case 2 :
    VCUComposition_demo_step2();
    break;

   case 3 :
    VCUComposition_demo_step3();
    break;

   case 4 :
    VCUComposition_demo_step4();
    break;

   case 5 :
    VCUComposition_demo_step5();
    break;

   case 6 :
    VCUComposition_demo_step6();
    break;

   case 7 :
    VCUComposition_demo_step7();
    break;

   default :
    break;
  }
}

/* Model initialize function */
void VCUComposition_demo_initialize(void)
{
  /* SystemInitialize for S-Function (fcgen): '<Root>/VCU_50msTask6' incorporates:
   *  SubSystem: '<Root>/Function-Call Subsystem3'
   */
  /* SystemInitialize for Chart: '<S1>/Chart' */
  VCUComposition_demo_DW.previousTicks = 0U;

  /* End of SystemInitialize for S-Function (fcgen): '<Root>/VCU_50msTask6' */

  /* SystemInitialize for S-Function (fcgen): '<S11>/100MS' incorporates:
   *  SubSystem: '<S11>/Function-Call Subsystem'
   */
  /* SystemInitialize for S-Function (ec5744_canreceiveslb): '<S15>/CANReceive' incorporates:
   *  SubSystem: '<S15>/Function-Call Subsystem'
   */
  /* System initialize for function-call system: '<S15>/Function-Call Subsystem' */

  /* SystemInitialize for Enabled SubSystem: '<S16>/Enabled Subsystem' */
  /* SystemInitialize for IfAction SubSystem: '<S17>/If Action Subsystem1' */
  /* Start for S-Function (ec5744_cantransmitslb): '<S22>/CANTransmit' incorporates:
   *  Constant: '<S22>/Constant'
   */
  ec_buffer_init(2,9,0,593U);

  /* End of SystemInitialize for SubSystem: '<S17>/If Action Subsystem1' */
  /* End of SystemInitialize for SubSystem: '<S16>/Enabled Subsystem' */
  ec_buffer_init(2,1,0,278);

  /* SystemInitialize for S-Function (fcgen): '<S14>/Function-Call Generator' incorporates:
   *  SubSystem: '<S14>/CCPBackground'
   */
  /* Start for S-Function (ec5744_ccpslb): '<S28>/CCPBackground' */
  ccpInit();

  /* End of SystemInitialize for S-Function (fcgen): '<S14>/Function-Call Generator' */

  /* SystemInitialize for S-Function (ec5744_caninterruptslb1): '<S14>/ReceiveandTransmitInterrupt' incorporates:
   *  SubSystem: '<S14>/CCPReceive'
   */
  /* System initialize for function-call system: '<S14>/CCPReceive' */

  /* SystemInitialize for S-Function (ec5744_canreceiveslb): '<S29>/CANReceive' */
  ec_buffer_init(2,0,0,CCP_CRO_ID);

  /* End of SystemInitialize for S-Function (ec5744_canreceiveslb): '<S29>/CANReceive' */
  ec_bufint_init(2,0);
  INTC_0.PSR[548].B.PRIN = 12;
  IntcIsrVectorTable[548] = (uint32_t)&ISR_FlexCAN_2_MB0;

  /* Enable for S-Function (fcgen): '<Root>/VCU_50msTask6' incorporates:
   *  SubSystem: '<Root>/Function-Call Subsystem3'
   */
  /* Enable for Chart: '<S1>/Chart' */
  VCUComposition_demo_DW.previousTicks =
    VCUComposition_demo_M->Timing.clockTick5;

  /* End of Enable for S-Function (fcgen): '<Root>/VCU_50msTask6' */
}

/* File trailer for ECUCoder generated file VCUComposition_demo.c.
 *
 * [EOF]
 */
